package com.sanpu.kxe.common.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.module.paypwd.adapter.CommonAdapter;
import com.sanpu.kxe.module.paypwd.model.KeybordModel;
import com.sanpu.kxe.module.paypwd.view.PswView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/14.
 */

public class InputPasswordView extends LinearLayout {

    private View rootView;

    @Bind(R.id.tv_title)
    TextView mTitle;
    @Bind(R.id.gv_keyboard)
    GridView mGridView;
    @Bind(R.id.pswView)
    PswView mPwdView;

    private final static int TEXT_WATCHER_LIMIT = 6;

    //当前密码
    private String mCurrPsw = "";

    public OnCompleteClickListener clickListener;

    public interface OnCompleteClickListener {
        void completeClick(String s);
    }

    public InputPasswordView(Context context) {
        super(context);
        initViews(context);
    }

    public InputPasswordView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    public InputPasswordView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews(context);
    }

    public void initViews(Context mContext) {
        rootView = View.inflate(mContext, R.layout.view_input_password, this);
        ButterKnife.bind(rootView);
        String[] keys = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", "delete"};
        String[] key_engs = new String[]{"", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ", "", "", "delete"};
        //构造数据
        List<KeybordModel> list = new ArrayList<>();
        for (int i = 0; i < key_engs.length; i++) {
            KeybordModel m = new KeybordModel();
            m.setKey(keys[i]);
            m.setKeyEng(key_engs[i]);
            list.add(m);
        }
        //实例化适配器
        GirdViewAdapter adapter = new GirdViewAdapter(mContext, list, R.layout.item_gridview_keyboard);
        mGridView.setAdapter(adapter);
        initEvent(list);
    }

    class GirdViewAdapter extends CommonAdapter<KeybordModel> {

        public GirdViewAdapter(Context context, List<KeybordModel> mDatas, int itemLayoutId) {
            super(context, mDatas, itemLayoutId);
        }

        @Override
        public void convert(ViewHolders holder, KeybordModel item, int position) {
            if (item.getKey().equals("delete")) {
                holder.getView(R.id.iv_delete).setVisibility(View.VISIBLE);
                holder.getView(R.id.rela_item).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_bg_selector_gray));
                holder.getView(R.id.ll_keys).setVisibility(View.INVISIBLE);
            } else if (TextUtils.isEmpty(item.getKey())) {
                holder.getView(R.id.iv_delete).setVisibility(View.GONE);
                holder.getView(R.id.rela_item).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_bg_selector_gray));
                holder.getView(R.id.ll_keys).setVisibility(View.INVISIBLE);
            } else {
                holder.getView(R.id.rela_item).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_bg_selector));
                holder.getView(R.id.iv_delete).setVisibility(View.GONE);
                holder.getView(R.id.ll_keys).setVisibility(View.VISIBLE);
                holder.setText(R.id.tv_key, item.getKey());
                holder.setText(R.id.tv_key_eng, item.getKeyEng());
            }
        }
    }

    public void setOnCompleteClickListener(OnCompleteClickListener listener) {
        this.clickListener = listener;
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title))
            mTitle.setText(title);
    }

    //给键盘设置事件监听
    private void initEvent(final List<KeybordModel> list) {
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!TextUtils.isEmpty(mCurrPsw) && mCurrPsw.length()==6)
                    return;
                //对空按钮不作处理
                if (i != 9) {
                    if (i == 11) {
                        //删除
                        if (mCurrPsw.length() > 0)
                            mCurrPsw = mCurrPsw.substring(0, mCurrPsw.length() - 1);
                        mPwdView.setDatas(mCurrPsw);
                    } else {
                        //非删除按钮
                        mCurrPsw += list.get(i).getKey();
                        //绘制当前密码
                        mPwdView.setDatas(mCurrPsw);
                        //当长度等于最大长度 表示密码输入完毕
                        if (mCurrPsw.length() == TEXT_WATCHER_LIMIT) {
                            clickListener.completeClick(mCurrPsw);
                        }
                    }
                }

            }
        });
    }

    public void clearPsw() {
        if (mPwdView != null) {
            mCurrPsw = "";
            mPwdView.setDatas("");
        }
    }
}
